package com.wbcs.config.dataexport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.wbcs.config.Config;
import com.wbcs.config.component.ComponentConfigLoadManager;
import com.wbcs.config.component.IComponentConfigBean;
import com.wbcs.config.component.container.AbsContainerConfigBean;
import com.wbcs.config.xml.XmlElementBean;
import com.wbcs.exception.WbcsConfigLoadingException;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.assistant.ComponentAssistant;
import com.wbcs.system.assistant.WbcsAssistant;
import com.wbcs.system.buttons.AbsButtonType;
import com.wbcs.system.buttons.DataExportButton;
import com.wbcs.util.Consts;
import com.wbcs.util.UniqueArrayList;

public class DataExportsConfigBean implements Cloneable
{
    private String filename;//导出的文件名，这里是配置的本组件所有数据导出默认的文件名，每个<dataexport/>还可以配置自己的filename覆盖这里的配置
    
    private Map<String,String> mDynFilename;
    
    private int batchselectcount=Integer.MIN_VALUE;
    
    private List<String> lstAutoDataExportTypes;

    private Map<String,AbsDataExportBean> mDataExportBeans;

    private IComponentConfigBean owner;

    public int getBatchselectcount()
    {
        return batchselectcount;
    }

    public DataExportsConfigBean(IComponentConfigBean owner)
    {
        this.owner=owner;
    }

    public String getFilename(ReportRequest rrequest)
    {
        String dataexporttype=ComponentAssistant.getInstance().getDataExportTypeByShowType(rrequest.getShowtype());
        String realfilename=null;
        if(this.mDataExportBeans!=null&&this.mDataExportBeans.get(dataexporttype)!=null)
        {
            realfilename=this.mDataExportBeans.get(dataexporttype).getFilename(rrequest);
        }
        if(realfilename!=null&&!realfilename.trim().equals("")) return realfilename;//在<dataexport/>中配置了自己的filename
        if(this.mDynFilename==null||this.mDynFilename.size()==0)
        {
            realfilename=this.filename;
        }else
        {
            realfilename=WbcsAssistant.getInstance().getRuntimeStringValueWithDynPart(rrequest,this.filename,this.mDynFilename);
        }
        return realfilename==null?"":rrequest.getI18NStringValue(realfilename.trim());
    }
    
    public Map<String,AbsDataExportBean> getMDataExportBeans()
    {
        return mDataExportBeans;
    }

    public void setMDataExportBeans(Map<String,AbsDataExportBean> dataExportBeans)
    {
        mDataExportBeans=dataExportBeans;
    }

    public List<String> getLstAutoDataExportTypes()
    {
        return lstAutoDataExportTypes;
    }

    public void setLstAutoDataExportTypes(List<String> lstAutoDataExportTypes)
    {
        if(lstAutoDataExportTypes==null)
        {
            this.lstAutoDataExportTypes=null;
        }else
        {
            List<String> lstTmp=new UniqueArrayList<String>();
            for(String typeTmp:lstAutoDataExportTypes)
            {
                typeTmp=typeTmp.toLowerCase().trim();
                if(typeTmp.equals("")) continue;
                if(typeTmp.equals(Consts.DATAEXPORT_NONE))
                {
                    lstTmp=null;
                    break;
                }else if(!Consts.lstDataExportTypes.contains(typeTmp))
                {
                    throw new WbcsConfigLoadingException("加载组件"+this.getOwner().getPath()+"失败，无效的数据导出类型："+typeTmp);
                }
                lstTmp.add(typeTmp);
            }
            if(lstTmp!=null&&lstTmp.size()==0) lstTmp=null;
            this.lstAutoDataExportTypes=lstTmp;
        }
    }

    public IComponentConfigBean getOwner()
    {
        return owner;
    }

    public void setOwner(IComponentConfigBean owner)
    {
        this.owner=owner;
    }

    public List<String> getLstIncludeApplicationids(String dataexporttype)
    {
        if(this.mDataExportBeans==null||!this.mDataExportBeans.containsKey(dataexporttype)) return null;
        return this.mDataExportBeans.get(dataexporttype).getLstIncludeApplicationids();
    }
    
    public List<String> getLstIncludeApplicationids(int showtype)
    {
        return getLstIncludeApplicationids(ComponentAssistant.getInstance().getDataExportTypeByShowType(showtype));
    }
    
    public String getIncludeApplicationids(String dataexporttype)
    {
        if(this.mDataExportBeans==null||!this.mDataExportBeans.containsKey(dataexporttype)) return null;
        return this.mDataExportBeans.get(dataexporttype).getIncludeApplicationids();
    }
    
    public String getIncludeApplicationids(int showtype)
    {
        return getIncludeApplicationids(ComponentAssistant.getInstance().getDataExportTypeByShowType(showtype));
    }
    
    public int getDataExportRecordcount(String reportid,String dataexporttype)
    {
        if(this.mDataExportBeans==null||!this.mDataExportBeans.containsKey(dataexporttype)) return -1;
        return this.mDataExportBeans.get(dataexporttype).getDataExportRecordcount(reportid);
    }
    
    public int getDataExportRecordcount(String reportid,int showtype)
    {
        return getDataExportRecordcount(reportid,ComponentAssistant.getInstance().getDataExportTypeByShowType(showtype));
    }

    public AbsDataExportBean getDataExportBean(String dataexporttype)
    {
        if(this.mDataExportBeans==null) return null;
        return this.mDataExportBeans.get(dataexporttype);
    }
    
    public AbsDataExportBean getDataExportBean(int showtype)
    {
        return getDataExportBean(ComponentAssistant.getInstance().getDataExportTypeByShowType(showtype));
    }
    
    public void loadConfig(XmlElementBean eleDataExports)
    {
        String batchselectcount=eleDataExports.attributeValue("batchselectcount");
        if(batchselectcount==null||batchselectcount.trim().equals(""))
        {
            this.batchselectcount=Config.getInstance().getDataexportBatchCount();
        }else
        {
            this.batchselectcount=Integer.parseInt(batchselectcount);
        }
        String filename=eleDataExports.attributeValue("filename");
        if(filename!=null)
        {
            this.mDynFilename=new HashMap<String,String>();
            this.filename=WbcsAssistant.getInstance().parseStringWithDynPart(filename,this.mDynFilename);
            if(this.mDynFilename.size()==0) this.mDynFilename=null;
        }
        List<XmlElementBean> lstEleChildren=eleDataExports.getLstChildElements();
        if(lstEleChildren==null||lstEleChildren.size()==0) return;
        mDataExportBeans=new HashMap<String,AbsDataExportBean>();
        AbsDataExportBean childDataExportBean;
        for(XmlElementBean eleChildTmp:lstEleChildren)
        {
            String type=eleChildTmp.attributeValue("type");
            type=type==null?"":type.toLowerCase().trim();
            if(type.equals(""))
            {
                throw new WbcsConfigLoadingException("加载组件"+this.owner.getPath()+"的数据导出<dataexports/>失败，必须指定<dataexport/>的type属性");
            }
            if(!Consts.lstDataExportTypes.contains(type))
            {
                throw new WbcsConfigLoadingException("加载组件"+this.owner.getPath()+"的数据导出<dataexports/>失败，<dataexport/>配置的type属性"+type+"不支持");
            }
            if(mDataExportBeans.containsKey(type))
            {
                throw new WbcsConfigLoadingException("加载组件"+this.owner.getPath()+"的数据导出<dataexports/>失败，<dataexport/>配置的type属性"+type+"存在重复");
            }
            childDataExportBean=createDataExportBean(type);
            childDataExportBean.loadConfig(eleChildTmp);
            mDataExportBeans.put(type,childDataExportBean);
        }
        if(mDataExportBeans.size()==0) mDataExportBeans=null;
    }

    private AbsDataExportBean createDataExportBean(String type)
    {
        if(type==null||type.trim().equals("")||!Consts.lstDataExportTypes.contains(type)) return null;
        if(type.equals(Consts.DATAEXPORT_PDF)) return new PDFExportBean(owner,type);
        if(type.equals(Consts.DATAEXPORT_PLAINEXCEL)) return new PlainExcelExportBean(owner,type);
        return new WordRichExcelExportBean(owner,type);
    }

    public void doPostLoad()
    {
        checkedAndAddButtons();
        if(mDataExportBeans==null) return;
        for(Entry<String,AbsDataExportBean> entryTmp:this.mDataExportBeans.entrySet())
        {
            entryTmp.getValue().doPostLoad();
        }
    }

    private void checkedAndAddButtons()
    {
        if(lstAutoDataExportTypes==null||lstAutoDataExportTypes.size()==0) return;
        List<AbsButtonType> lstDataExportButtons=null;
        int i=1;
        for(String dataexportTypeTmp:lstAutoDataExportTypes)
        {
            lstDataExportButtons=null;
            if(this.owner.getButtonsBean()!=null) lstDataExportButtons=this.owner.getButtonsBean().getLstDataExportTypeButtons(dataexportTypeTmp);//取到已经配置的所有这种类型的导出按钮
            if(!isHasCertainTypeDataExportButton(lstDataExportButtons))
            {//此报表没有配置这种类型的数据导出按钮（注意这里不包括那些在<button/>标签内容中指定了自己导出应用ID的按钮）
                AbsButtonType buttonObj=Config.getInstance().getResourceButton(null,this.getOwner(),
                        Consts.M_DATAEXPORT_DEFAULTBUTTONS.get(dataexportTypeTmp),DataExportButton.class);
                if(buttonObj.getName()==null||buttonObj.getName().trim().equals(""))
                {
                    buttonObj.setName(DataExportButton.class.getName()+(i++)+"."+(int)(Math.random()*10000));
                }
                if(this.owner instanceof AbsContainerConfigBean)
                {
                    buttonObj.setPosition("top");
                }
                ComponentConfigLoadManager.addButtonToPositions(this.getOwner(),buttonObj);
            }
        }
        lstAutoDataExportTypes=null;
    }

    private boolean isHasCertainTypeDataExportButton(List<AbsButtonType> lstDataExportButtons)
    {
        if(lstDataExportButtons==null||lstDataExportButtons.size()==0) return false;
        DataExportButton debtnTmp;
        for(AbsButtonType buttonObjTmp:lstDataExportButtons)
        {
            debtnTmp=(DataExportButton)buttonObjTmp;
            if(!debtnTmp.isExportBySpecifyApplicationids()) return true;
        }
        return false;
    }

    public DataExportsConfigBean clone(IComponentConfigBean owner)
    {
        try
        {
            DataExportsConfigBean newBean=(DataExportsConfigBean)super.clone();
            newBean.setOwner(owner);
            if(lstAutoDataExportTypes!=null) newBean.setLstAutoDataExportTypes((List<String>)((ArrayList<String>)lstAutoDataExportTypes).clone());
            if(mDataExportBeans!=null)
            {
                Map<String,AbsDataExportBean> mDataExportBeansNew=new HashMap<String,AbsDataExportBean>();
                for(Entry<String,AbsDataExportBean> entryTmp:this.mDataExportBeans.entrySet())
                {
                    mDataExportBeansNew.put(entryTmp.getKey(),entryTmp.getValue().clone(owner));
                }
                newBean.setMDataExportBeans(mDataExportBeansNew);
            }
            return newBean;
        }catch(CloneNotSupportedException e)
        {
            throw new WbcsConfigLoadingException("clone组件"+this.owner.getPath()+"的数据导出对象失败",e);
        }
    }
}
